// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Misc/FileHelper.h"
#include "CommonLibrary.generated.h"


#define DEBUG_MESSAGE(Message, ...) \
if (GEngine) {\
	GEngine->AddOnScreenDebugMessage(-1, 20.f, FColor::Green, FString::Printf(TEXT("File: %s(%d)"),*FString(__FILE__),__LINE__));\
	GEngine->AddOnScreenDebugMessage(-1, 20.f, FColor::Green, FString::Printf(TEXT(Message), ##__VA_ARGS__)); \
}

#define CZ_LOG_S(FileName,Value) \
{ \
    static bool IsNewLog = true; \
    FString SaveDirectory = FPaths::ProjectLogDir(); \
    SaveDirectory.Append("Logs/"); \
    FString TmpFileName = FileName; \
    TmpFileName.Append(".log.txt"); \
    \
    /** CreateDirectoryTree returns true if the destination directory existed prior to call or has been created during the call.*/ \
    IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile(); \
    CA_CONSTANT_IF(!PlatformFile.DirectoryExists(*SaveDirectory)) \
        PlatformFile.CreateDirectoryTree(*SaveDirectory); \
    \
        /** Get absolute file path*/ \
        FString AbsoluteFilePath = SaveDirectory + "/" + TmpFileName; \
        FFileHelper::EEncodingOptions EncodingOptions = FFileHelper::EEncodingOptions::ForceAnsi; \
        IFileManager* FileManager = &IFileManager::Get(); \
        uint32 WriteFlags = (uint32)(EFileWrite::FILEWRITE_Append); \
        /** if file already exist,change name and save  it.*/ \
        CA_CONSTANT_IF(PlatformFile.FileExists(*AbsoluteFilePath) && IsNewLog) \
        { \
            IsNewLog = false; \
            FString AbsoluteDestinationPath = SaveDirectory + "/" + FDateTime::Now().ToString() + "-" + TmpFileName; \
            PlatformFile.MoveFile(*AbsoluteDestinationPath, *AbsoluteFilePath); \
            PlatformFile.DeleteFile(*AbsoluteFilePath); \
        } \
        { \
            FString TextToSave = FDateTime::Now().ToString(); \
            TextToSave.Append("\r\n"); \
            TextToSave.Append(FString::Printf(TEXT("Date: %s %s"),*FString(__DATE__),*FString(__TIME__))); \
            TextToSave.Append("\r\n"); \
            TextToSave.Append(FString::Printf(TEXT("File: %s(%d)"),*FString(__FILE__),__LINE__)); \
            TextToSave.Append("\r\n\t"); \
            TextToSave.Append(Value); \
            TextToSave.Append("\r\n\n"); \
            FFileHelper::SaveStringToFile(TextToSave, *AbsoluteFilePath, EncodingOptions, FileManager, WriteFlags); \
        } \
    \
}

UCLASS()
class MISCELLANEOUSPROJECT_API UCommonLibrary : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
public:
	UFUNCTION(BlueprintCallable)
	static void ShowMessageOnScreen(const FString& Message);

    UFUNCTION(BlueprintCallable)
    static class UWidgetAnimation* GetAnimInstance(TSubclassOf<UUserWidget> WidgetClass,const FText AnimName);
};